public class Calculator {
    public static void main(String[] args) {
        CalArrayStack numStack = new CalArrayStack(10);
        CalArrayStack operStack = new CalArrayStack(10);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int result = 0;
        char ch = ' ';
        String express = "2+5*6-2";
        while(true){
            ch = express.substring(index,index+1).charAt(0);
            if(operStack.isOper(ch)){
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch) <= operStack.priority(operStack.peek())){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        result = numStack.cal(num1,num2,oper);
                        numStack.push(result);
                        operStack.push(ch);
                    }else{
                        operStack.push(ch);
                    }
                }else{
                    numStack.push(ch - 48);
                }
                index++;
                if(index >= express.length()){
                    break;
                }
            }
        }
        while(true) {
            //如果符号栈为空，则计算到最后的结果, 数栈中只有一个数字【结果】
            if(operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            result = numStack.cal(num1, num2, oper);
            numStack.push(result);//入栈
        }
        //将数栈的最后数，pop出，就是结果
        int res2 = numStack.pop();
        System.out.printf("表达式 %s = %d", express, res2);
    }
}

class CalArrayStack{
    int top = -1;
    int capacity;
    int[] array;

    public CalArrayStack(int capacity){
        this.capacity = capacity;
        array = new int[this.capacity];
    }

    boolean isFull(){
        return (top == capacity - 1);
    }

    boolean isEmpty(){
        return (top == -1);
    }

    void push(int data){
        if(isFull()){
            throw new RuntimeException("栈已满");
        }else{
            array[++top] = data;
        }
    }

    int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空");
        }else{
            return array[top--];
        }
    }

    int peek(){
        return array[top];
    }

    int priority(int oper){
        if(oper == '*' || oper == '/' ){
            return 1;
        }else if(oper == '+' || oper == '-'){
            return 0;
        }else{
            return -1;
        }
    }

    boolean isOper(char oper){
        return (oper == '*' || oper == '/' || oper == '+' || oper == '-');
    }

    int cal(int num1, int num2, int oper){
        int result = 0;
        switch (oper){
            case '+' : result = num1 + num2;break;
            case '-' : result = num2 - num1;break;
            case '*' : result = num1 * num2;break;
            case '/' : result = num2 / num1;break;
            default:break;
        }
        return result;
    }
}